.class public Lcom/opongo/blogapp/utils/FileCache;
.super Ljava/lang/Object;
.source "FileCache.java"


# static fields
.field public static final DEBUG:Z

.field private static final TAG:Ljava/lang/String;


# instance fields
.field private cacheDir:Ljava/io/File;


# direct methods
.method static constructor <clinit>()V
    .locals 1

    .prologue
    .line 15
    sget-boolean v0, Lcom/opongo/blogapp/AbaSettings;->DEBUG:Z

    sput-boolean v0, Lcom/opongo/blogapp/utils/FileCache;->DEBUG:Z

    .line 16
    const-class v0, Lcom/opongo/blogapp/utils/FileCache;

    invoke-virtual {v0}, Ljava/lang/Class;->getSimpleName()Ljava/lang/String;

    move-result-object v0

    sput-object v0, Lcom/opongo/blogapp/utils/FileCache;->TAG:Ljava/lang/String;

    .line 12
    return-void
.end method

.method public constructor <init>(Landroid/content/Context;)V
    .locals 5
    .parameter "context"

    .prologue
    .line 20
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    .line 21
    invoke-static {}, Landroid/os/Environment;->getExternalStorageState()Ljava/lang/String;

    move-result-object v1

    .line 22
    .local v1, sdState:Ljava/lang/String;
    const-string v2, "mounted"

    invoke-virtual {v1, v2}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z

    move-result v2

    if-eqz v2, :cond_2

    .line 23
    invoke-static {}, Landroid/os/Environment;->getExternalStorageDirectory()Ljava/io/File;

    move-result-object v0

    .line 24
    .local v0, sdCard:Ljava/io/File;
    invoke-virtual {v0}, Ljava/io/File;->canWrite()Z

    move-result v2

    if-eqz v2, :cond_1

    .line 25
    new-instance v2, Ljava/io/File;

    new-instance v3, Ljava/lang/StringBuilder;

    const-string v4, "data/"

    invoke-direct {v3, v4}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V

    .line 26
    invoke-virtual {p1}, Landroid/content/Context;->getPackageName()Ljava/lang/String;

    move-result-object v4

    invoke-virtual {v3, v4}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v3

    invoke-virtual {v3}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v3

    invoke-direct {v2, v0, v3}, Ljava/io/File;-><init>(Ljava/io/File;Ljava/lang/String;)V

    .line 25
    iput-object v2, p0, Lcom/opongo/blogapp/utils/FileCache;->cacheDir:Ljava/io/File;

    .line 33
    .end local v0           #sdCard:Ljava/io/File;
    :goto_0
    iget-object v2, p0, Lcom/opongo/blogapp/utils/FileCache;->cacheDir:Ljava/io/File;

    invoke-virtual {v2}, Ljava/io/File;->exists()Z

    move-result v2

    if-nez v2, :cond_0

    .line 34
    iget-object v2, p0, Lcom/opongo/blogapp/utils/FileCache;->cacheDir:Ljava/io/File;

    invoke-virtual {v2}, Ljava/io/File;->mkdirs()Z

    .line 35
    :cond_0
    return-void

    .line 28
    .restart local v0       #sdCard:Ljava/io/File;
    :cond_1
    sget-object v2, Lcom/opongo/blogapp/utils/FileCache;->TAG:Ljava/lang/String;

    const-string v3, "SDCard is not writable!"

    invoke-static {v2, v3}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I

    goto :goto_0

    .line 31
    .end local v0           #sdCard:Ljava/io/File;
    :cond_2
    invoke-virtual {p1}, Landroid/content/Context;->getCacheDir()Ljava/io/File;

    move-result-object v2

    iput-object v2, p0, Lcom/opongo/blogapp/utils/FileCache;->cacheDir:Ljava/io/File;

    goto :goto_0
.end method

.method private clear(Ljava/io/File;I)I
    .locals 15
    .parameter "dir"
    .parameter "numDays"

    .prologue
    .line 51
    const/4 v2, 0x0

    .line 53
    .local v2, deletedFiles:I
    if-eqz p1, :cond_0

    invoke-virtual/range {p1 .. p1}, Ljava/io/File;->isDirectory()Z

    move-result v4

    if-eqz v4, :cond_0

    .line 55
    :try_start_0
    invoke-virtual/range {p1 .. p1}, Ljava/io/File;->listFiles()[Ljava/io/File;

    move-result-object v5

    array-length v6, v5

    const/4 v4, 0x0

    :goto_0
    if-lt v4, v6, :cond_1

    .line 75
    :cond_0
    :goto_1
    return v2

    .line 55
    :cond_1
    aget-object v1, v5, v4

    .line 58
    .local v1, child:Ljava/io/File;
    invoke-virtual {v1}, Ljava/io/File;->isDirectory()Z

    move-result v7

    if-eqz v7, :cond_2

    .line 59
    move/from16 v0, p2

    invoke-direct {p0, v1, v0}, Lcom/opongo/blogapp/utils/FileCache;->clear(Ljava/io/File;I)I

    move-result v7

    add-int/2addr v2, v7

    .line 62
    :cond_2
    invoke-virtual {v1}, Ljava/io/File;->lastModified()J

    move-result-wide v7

    new-instance v9, Ljava/util/Date;

    invoke-direct {v9}, Ljava/util/Date;-><init>()V

    invoke-virtual {v9}, Ljava/util/Date;->getTime()J

    move-result-wide v9

    move/from16 v0, p2

    int-to-long v11, v0

    const-wide/32 v13, 0x5265c00

    mul-long/2addr v11, v13

    sub-long/2addr v9, v11

    cmp-long v7, v7, v9

    if-gez v7, :cond_4

    .line 63
    invoke-virtual {v1}, Ljava/io/File;->delete()Z

    move-result v7

    if-eqz v7, :cond_4

    .line 64
    sget-boolean v7, Lcom/opongo/blogapp/utils/FileCache;->DEBUG:Z

    if-eqz v7, :cond_3

    sget-object v7, Lcom/opongo/blogapp/utils/FileCache;->TAG:Ljava/lang/String;

    new-instance v8, Ljava/lang/StringBuilder;

    const-string v9, "Deleting file: "

    invoke-direct {v8, v9}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V

    invoke-virtual {v1}, Ljava/io/File;->getName()Ljava/lang/String;

    move-result-object v9

    invoke-virtual {v8, v9}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v8

    invoke-virtual {v8}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v8

    invoke-static {v7, v8}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    .line 65
    :cond_3
    add-int/lit8 v2, v2, 0x1

    .line 55
    :cond_4
    add-int/lit8 v4, v4, 0x1

    goto :goto_0

    .line 70
    .end local v1           #child:Ljava/io/File;
    :catch_0
    move-exception v3

    .line 71
    .local v3, e:Ljava/lang/Exception;
    sget-boolean v4, Lcom/opongo/blogapp/utils/FileCache;->DEBUG:Z

    if-eqz v4, :cond_0

    .line 72
    sget-object v4, Lcom/opongo/blogapp/utils/FileCache;->TAG:Ljava/lang/String;

    const-string v5, "Failed to clean the cache, error %s"

    const/4 v6, 0x1

    new-array v6, v6, [Ljava/lang/Object;

    const/4 v7, 0x0

    invoke-virtual {v3}, Ljava/lang/Exception;->getMessage()Ljava/lang/String;

    move-result-object v8

    aput-object v8, v6, v7

    invoke-static {v5, v6}, Ljava/lang/String;->format(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;

    move-result-object v5

    invoke-static {v4, v5}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;)I

    goto :goto_1
.end method


# virtual methods
.method public clear(I)I
    .locals 3
    .parameter "numDays"

    .prologue
    .line 44
    sget-boolean v0, Lcom/opongo/blogapp/utils/FileCache;->DEBUG:Z

    if-eqz v0, :cond_0

    sget-object v0, Lcom/opongo/blogapp/utils/FileCache;->TAG:Ljava/lang/String;

    new-instance v1, Ljava/lang/StringBuilder;

    const-string v2, "Clearing files from cache older than "

    invoke-direct {v1, v2}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V

    invoke-static {p1}, Ljava/lang/String;->valueOf(I)Ljava/lang/String;

    move-result-object v2

    invoke-virtual {v1, v2}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v1

    const-string v2, " days"

    invoke-virtual {v1, v2}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v1

    invoke-virtual {v1}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v1

    invoke-static {v0, v1}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I

    .line 46
    :cond_0
    iget-object v0, p0, Lcom/opongo/blogapp/utils/FileCache;->cacheDir:Ljava/io/File;

    invoke-direct {p0, v0, p1}, Lcom/opongo/blogapp/utils/FileCache;->clear(Ljava/io/File;I)I

    move-result v0

    return v0
.end method

.method public getFile(Ljava/lang/String;)Ljava/io/File;
    .locals 3
    .parameter "url"

    .prologue
    .line 38
    invoke-static {p1}, Lcom/opongo/blogapp/utils/DigestUtils;->md5(Ljava/lang/String;)Ljava/lang/String;

    move-result-object v1

    .line 39
    .local v1, filename:Ljava/lang/String;
    new-instance v0, Ljava/io/File;

    iget-object v2, p0, Lcom/opongo/blogapp/utils/FileCache;->cacheDir:Ljava/io/File;

    invoke-direct {v0, v2, v1}, Ljava/io/File;-><init>(Ljava/io/File;Ljava/lang/String;)V

    .line 40
    .local v0, f:Ljava/io/File;
    return-object v0
.end method
